package com.ujoin.dao.imp;

import java.sql.Timestamp;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ujoin.bean.UserMessage;
import com.ujoin.dao.IUserMessageDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * UserMessage entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.ujoin.bean.UserMessage
 * @author MyEclipse Persistence Tools
 */

public class UserMessageDAO extends HibernateDaoSupport implements IUserMessageDAO {
	private static final Logger log = LoggerFactory
			.getLogger(UserMessageDAO.class);
	// property constants
	public static final String RECEIVER_ID = "receiverId";
	public static final String SENDER_ID = "senderId";
	public static final String MESSAGE = "message";
	public static final String IS_READ = "isRead";
	public static final String VALID = "valid";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#save(com.ujoin.bean.UserMessage)
	 */
	@Override
	public void save(UserMessage transientInstance) {
		log.debug("saving UserMessage instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#delete(com.ujoin.bean.UserMessage)
	 */
	@Override
	public void delete(UserMessage persistentInstance) {
		log.debug("deleting UserMessage instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findById(java.lang.Integer)
	 */
	@Override
	public UserMessage findById(java.lang.Integer id) {
		log.debug("getting UserMessage instance with id: " + id);
		try {
			UserMessage instance = (UserMessage) getHibernateTemplate().get(
					"com.ujoin.bean.UserMessage", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findByExample(com.ujoin.bean.UserMessage)
	 */
	@Override
	public List findByExample(UserMessage instance) {
		log.debug("finding UserMessage instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding UserMessage instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from UserMessage as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findByReceiverId(java.lang.Object)
	 */
	@Override
	public List findByReceiverId(Object receiverId) {
		return findByProperty(RECEIVER_ID, receiverId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findBySenderId(java.lang.Object)
	 */
	@Override
	public List findBySenderId(Object senderId) {
		return findByProperty(SENDER_ID, senderId);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findByMessage(java.lang.Object)
	 */
	@Override
	public List findByMessage(Object message) {
		return findByProperty(MESSAGE, message);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findByIsRead(java.lang.Object)
	 */
	@Override
	public List findByIsRead(Object isRead) {
		return findByProperty(IS_READ, isRead);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findByValid(java.lang.Object)
	 */
	@Override
	public List findByValid(Object valid) {
		return findByProperty(VALID, valid);
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all UserMessage instances");
		try {
			String queryString = "from UserMessage";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#merge(com.ujoin.bean.UserMessage)
	 */
	@Override
	public UserMessage merge(UserMessage detachedInstance) {
		log.debug("merging UserMessage instance");
		try {
			UserMessage result = (UserMessage) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#attachDirty(com.ujoin.bean.UserMessage)
	 */
	@Override
	public void attachDirty(UserMessage instance) {
		log.debug("attaching dirty UserMessage instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.ujoin.dao.IUserMessageDAO#attachClean(com.ujoin.bean.UserMessage)
	 */
	@Override
	public void attachClean(UserMessage instance) {
		log.debug("attaching clean UserMessage instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IUserMessageDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (IUserMessageDAO) ctx.getBean("UserMessageDAO");
	}
}